<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" :label="$t('common.keywords')">
          <el-input
            v-model="queryParams.keywords"
            :placeholder="
              $t('visitor.name') + ', ' + $t('visitor.companyName') + ', ' + $t('visitor.phone')
            "
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">
            {{ $t("common.search") }}
          </el-button>
          <el-button icon="refresh" @click="handleResetQuery">{{ $t("common.reset") }}</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table ref="dataTableRef" v-loading="loading" :data="list" highlight-current-row stripe>
        <el-table-column :label="$t('visitor.name')" prop="name" />
        <el-table-column :label="$t('visitor.companyName')" prop="companyName" />
        <el-table-column :label="$t('visitor.phone')" prop="phone" />
        <el-table-column :label="$t('visitor.opName')" align="center" prop="opName" />
        <el-table-column :label="$t('common.createdAt')" prop="createdAt">
          <template #default="scope">
            {{ dayjs(scope.row.createdAt).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import dayjs from "dayjs";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
defineOptions({
  name: "Visitors",
  inheritAttrs: false,
});

import VisitorApi, { IPageQuery, IPageVO } from "@/api/visitor/index";
const screenWidth = window.innerWidth;
const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<IPageQuery>({
  pageNum: 1,
  pageSize: 10,
  keywords: "",
  status: undefined,
});

const list = ref<IPageVO[]>();

function handleQuery() {
  loading.value = true;
  VisitorApi.getList(queryParams)
    .then((data) => {
      list.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}
onMounted(() => {
  handleQuery();
});
</script>
